package com.sunmouse.toolkit.fragment.ftp;


import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunmouse.toollib.adapter.OnChildClickListener;
import com.sunmouse.toollib.receiver.OnReceiveListener;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.adapter.HomePagerAdapter;
import com.sunmouse.toolkit.anim.MenuAnim;
import com.sunmouse.toolkit.receiver.SwitchReceiver;
import com.sunmouse.toolkit.constant.Config;
import com.sunmouse.toolkit.constant.Value;
import com.sunmouse.toolkit.fragment.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class FTPFragment extends BaseFragment {


    @BindView(R.id.m_vp)
    ViewPager mVp;
    Unbinder unbinder;
    private ArrayList<Fragment> fragments;
    private HomePagerAdapter mAdapter;
    private SwitchReceiver mReceiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_ftp, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void init() {
        mReceiver = new SwitchReceiver();
        mReceiver.setOnReceiveListener(mOnReceiveListener);
        getActivity().registerReceiver(mReceiver, new IntentFilter(Value.Action.SWITCH));
        fragments = new ArrayList<>();
        fragments.add(new FtpMenuFragment());
        FileListFragment mFileListFragment = new FileListFragment();
        mFileListFragment.setOnChildClickListener(mOnChildClickListener);
        fragments.add(mFileListFragment);
        mAdapter = new HomePagerAdapter(mManager, fragments);

        this.mVp.setAdapter(mAdapter);

        this.mVp.setPageTransformer(true, new MenuAnim());
        this.mVp.setCurrentItem(1);
    }

    /**
     * switch receiver
     */
    private OnReceiveListener mOnReceiveListener = new OnReceiveListener() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int index = intent.getIntExtra(Config.Key.INDEX, -1);

            switch (index) {
                case 0:
                    mVp.setCurrentItem(0);
                    break;
                case 1:
                    mVp.setCurrentItem(1);
                    break;
            }

        }
    };


    private OnChildClickListener mOnChildClickListener = new OnChildClickListener() {
        @Override
        public void onClick(View v, int position) {
            int currentPage = mVp.getCurrentItem();
            if (currentPage == 1) {
                mVp.setCurrentItem(0);
            } else {
                mVp.setCurrentItem(1);
            }
        }
    };



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        getActivity().unregisterReceiver(mReceiver);
    }
}
